﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Web;
using MarsExplorerWebSite.Model;

namespace MarsExplorerWebSite.Functions
{
    public class resetGame : FunctionBase
    {
        private MarsExplorerEntities dbContext;

        public resetGame(HttpContext context) : base(context)
        {
            InitializeProperties();
        }

        public override string Execute()
        {
            var t = from x in dbContext.Teams select x;
            foreach (Team z in t)
            {
                var r = from f in dbContext.TeamsResources where f.TeamID == z.TeamID select f;
                foreach (TeamsResource h in r)
                {
                    h.Value = 0;
                }

                var e = from q in dbContext.TeamsTurns where q.TeamID == z.TeamID select q;
                foreach (TeamsTurn m in e)
                {
                    m.Value = 0;
                }
                z.RouteID = 1;
                z.Credits = 1550000;
                z.Minerals = 0;
                z.Mining = 0;
                z.canMove = 0;
            }

            Game g = (from x in dbContext.Games select x).FirstOrDefault();
            g.RunningTurn = 1;
            dbContext.SaveChanges();
            return "OK";
        }

        public override void InitializeProperties()
        {
            try
            {
                dbContext = new MarsExplorerEntities();
            }
            catch (Exception e)
            {
                string message = "";
                #if DEBUG
                message += " : " + e.ToString();
                #endif
            }
        }
    }
}